package com.ss.android.newmedia.message;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Looper;
import android.text.TextUtils;

import com.bytedance.common.plugin.interfaces.pushmanager.PushSettingManager;
import com.bytedance.common.plugin.interfaces.pushmanager.app.ShutPushType;
import com.bytedance.common.plugin.interfaces.pushmanager.client.SsPushManager;
import com.bytedance.common.plugin.interfaces.pushmanager.client.SsRedbadgeManager;
import com.bytedance.common.plugin.interfaces.pushmanager.setting.LocalHttpSetting;
import com.bytedance.common.plugin.interfaces.pushmanager.setting.PushSetting;
import com.bytedance.common.plugin.interfaces.pushmanager.setting.RedbadgeSetting;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.c.SharedPreferenceCompat;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.frameworks.b.a.ComponentsManifest;
import com.ss.android.bb;
import com.ss.android.common.app.AbsApplication;
import com.ss.android.common.applog.AppLog;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.newmedia.BaseAppData;
import com.ss.android.newmedia.NewMediaConstants;
import com.ss.android.newmedia.message.dialog.w;
import com.ss.android.push.window.oppo.OppoPushWindowManager;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

public final class c implements bb {
    public static boolean a;
    protected static int b;
    public static ShutPushType c;
    protected String d;
    protected int e;
    protected int f;
    protected int g;
    protected int h;
    int i;
    protected String j;
    public static CountDownLatch k;
    private int l;
    private int m;
    private int n;
    private int o;
    private int p;
    private int q;
    private int r;
    private String s;
    private boolean t;
    private boolean u;
    private Context v;
    private static c w;

    static {
        a = true;
        b = -1;
        c = ShutPushType.BACK_CONTROL;
        new WeakHandler(Looper.getMainLooper(), new com.ss.android.newmedia.message.d());
        k = new CountDownLatch(1);
    }

    private c() {
        this.d = NewMediaConstants.questionnaireUninstallUrl;
        int v0 = a ? 1 : 0;
        this.e = v0;
        v0 = l() == ShutPushType.CLOSE_SERVICE ? 1 : 0;
        this.f = v0;
        this.g = 1;
        this.h = 1;
        this.l = -1;
        this.m = 10800;
        this.n = 1;
        this.i = 1;
        this.o = 1;
        this.p = 1;
        this.q = 1;
        this.r = 1;
        this.j = "";
        this.s = "";
        this.t = true;
        this.u = false;
        com.bytedance.frameworks.b.a.a.a(b.class, this);
        this.v = AbsApplication.getApplication().getContext();
        if (this.e()) {
            try {
                Intent v0_2 = new Intent(this.v, MessageHandler.class);
                v0_2.setAction("com.ss.android.newmedia.message.cache.action");
                this.v.startService(v0_2);
            } catch (Throwable v0_1) {
            }
        }
    }

    public static c a() {
        c v0_1;
        Class v1 = c.class;
        synchronized (v1) {
            if (w == null) {
                w = new c();
            }

            v0_1 = w;
        }
        return v0_1;
    }

    public final void a(Boolean arg4) {
        this.u = true;
        PushSettingManager.getInstance().notifyPushEnableChange(this.v, arg4.booleanValue());
        this.t = arg4.booleanValue();
        Object v0 = ComponentsManifest.findComponent(com.ss.android.cc.class);
        if (v0 != null) {
            SharedPreferences.Editor v0_1 = ((com.ss.android.cc) v0).getAppSettingSp().edit();
            v0_1.putBoolean("notify_enabled", this.t);
            SharedPreferenceCompat.commit(v0_1);
        }
    }

    static Context a(c arg1) {
        return arg1.v;
    }

    public final void a(SharedPreferences.Editor arg4) {
        arg4.putBoolean("notify_enabled", this.t);
        arg4.putInt("allow_message_cache", this.n);
        arg4.putInt("allow_message_big_style", this.o);
        arg4.putInt("tt_allow_push_stick_top", this.i);
        arg4.putInt("allow_message_small_style", this.p);
        arg4.putInt("allow_push_daemon_monitor", this.g);
        arg4.putInt("tt_allow_custom_message_big_style", this.q);
        arg4.putInt("tt_allow_custom_message_small_style", this.r);
        arg4.putInt("tt_is_not_applog_request_push_sender", this.l);
        arg4.putString("tt_push_cache_rule", this.s);
        new com.ss.android.newmedia.i.b(com.ss.android.newmedia.i.a.a(this.v)).start();
        arg4.putString("tt_push_alert_dialog_setting", com.ss.android.newmedia.message.dialog.w.a(this.v).a);
        OppoPushWindowManager v0 = OppoPushWindowManager.a(this.v);
        arg4.putString("tt_push_pop_window_rule", v0.f);
        v0.a();
        try {
            if (TextUtils.isEmpty(this.s)) {
//                goto label_54;
            } else {
                com.ss.android.newmedia.message.cache.a.a().a(new JSONObject(this.s));
            }
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
        }

        label_54:
        this.m();
    }

    public final void a(SharedPreferences arg5) {
        int v1 = 1;
        int v0 = PushSetting.getInstance().isShutPushOnStopService() ? 1 : 0;
        this.f = v0;
        v0 = PushSetting.getInstance().isAllowSettingsNotifyEnable() ? 1 : 0;
        b = v0;
        boolean v0_1 = v0 == 1 ? true : false;
        a = v0_1;
        this.d = PushSetting.getInstance().getUninstallQuestionUrl();
        v0 = PushSetting.getInstance().isAllowPushJobService() ? 1 : 0;
        this.e = v0;
        v0 = PushSetting.getInstance().isAllowOffAlive() ? 1 : 0;
        this.h = v0;
        this.j = PushSetting.getInstance().getWakeupBlackListPackages();
        v0 = PushSetting.getInstance().isAllowPushDaemonMonitor() ? 1 : 0;
        this.g = v0;
        this.m = PushSetting.getInstance().getUpdateSenderIntervalTimeSecond();
        this.n = arg5.getInt("allow_message_cache", 1);
        this.o = arg5.getInt("allow_message_big_style", 1);
        this.i = arg5.getInt("tt_allow_push_stick_top", 0);
        this.p = arg5.getInt("allow_message_small_style", 1);
        this.q = arg5.getInt("tt_allow_custom_message_big_style", 1);
        this.r = arg5.getInt("tt_allow_custom_message_small_style", 1);
        this.l = arg5.getInt("tt_is_not_applog_request_push_sender", 1);
        this.s = arg5.getString("tt_push_cache_rule", "");
        this.t = arg5.getBoolean("notify_enabled", true);
        PushSetting.getInstance().setPushNotifyEnable(this.t);
        com.ss.android.newmedia.i.a v0_2 = com.ss.android.newmedia.i.a.a(this.v);
        if (!RedbadgeSetting.getInstance(v0_2.c).isDesktopRedBadgeShow()) {
            v1 = 0;
        }

        v0_2.a = v1;
        v0_2.b = RedbadgeSetting.getInstance(v0_2.c).getDesktopRedBadgeArgs();
        OppoPushWindowManager v0_3 = OppoPushWindowManager.a(this.v);
        v0_3.f = arg5.getString("tt_push_pop_window_rule", "");
        v0_3.a();
        w v0_4 = com.ss.android.newmedia.message.dialog.w.a(this.v);
        v0_4.a = arg5.getString("tt_push_alert_dialog_setting", "");
        v0_4.a();
        try {
            if (TextUtils.isEmpty(this.s)) {
                return;
            }

            com.ss.android.newmedia.message.cache.a.a().a(new JSONObject(this.s));
        } catch (Exception v0_5) {
            v0_5.printStackTrace();
        }
    }

    public final void a(boolean arg5) {
        if ((arg5) || (this.u)) {
            Object v0 = ComponentsManifest.findComponent(com.ss.android.cc.class);
            if (v0 != null) {
                String v3 = "apn_notify";
                int v1 = this.t ? 1 : 0;
                ((com.ss.android.cc) v0).a(v3, v1);
                ((com.ss.android.cc) v0).a("notification_enable", ToolUtils.g(this.v));
            }
        }

        this.u = false;
    }

    public final boolean a(JSONObject arg9) {
        int v2 = 0;
        String v3 = "shut_push_on_stop_service";
        int v0 = l() == ShutPushType.CLOSE_SERVICE ? 1 : 0;
        v0 = arg9.optInt(v3, v0);
        if (v0 == this.f || v0 < 0) {
            v0 = 0;
        } else {
            this.f = v0;
            v0 = 1;
        }

        String v4 = "allow_settings_notify_enable";
        int v3_1 = a ? 1 : 0;
        v3_1 = arg9.optInt(v4, v3_1);
        if (v3_1 != b && v3_1 >= 0) {
            b = v3_1;
            v0 = 1;
        }

        v3 = arg9.optString("uninstall_question_url", NewMediaConstants.questionnaireUninstallUrl);
        if (v3 != null && !v3.equals(this.d)) {
            this.d = v3;
            v0 = 1;
        }

        v4 = "allow_push_job_service";
        v3_1 = a ? 1 : 0;
        v3_1 = arg9.optInt(v4, v3_1);
        if (v3_1 != this.e && v3_1 >= 0) {
            this.e = v3_1;
            v0 = 1;
        }

        v3_1 = arg9.optInt("allow_message_cache", 1);
        if (v3_1 != this.n && v3_1 >= 0) {
            this.n = v3_1;
            v0 = 1;
        }

        v3_1 = arg9.optInt("allow_message_big_style", 1);
        if (v3_1 != this.o && v3_1 >= 0) {
            this.o = v3_1;
            v0 = 1;
        }

        v3_1 = arg9.optInt("tt_allow_push_stick_top", 0);
        if (v3_1 != this.i && v3_1 >= 0) {
            this.i = v3_1;
            v0 = 1;
        }

        v3_1 = arg9.optInt("allow_message_small_style", 1);
        if (v3_1 != this.p && v3_1 >= 0) {
            this.p = v3_1;
            v0 = 1;
        }

        v3_1 = arg9.optInt("tt_allow_custom_message_big_style", 1);
        if (v3_1 != this.q && v3_1 >= 0) {
            this.q = v3_1;
            v0 = 1;
        }

        v3_1 = arg9.optInt("tt_allow_custom_message_small_style", 1);
        if (v3_1 != this.r && v3_1 >= 0) {
            this.r = v3_1;
            v0 = 1;
        }

        v3 = arg9.optString("tt_push_cache_rule");
        if (!TextUtils.isEmpty(((CharSequence) v3)) && !this.s.equals(v3)) {
            this.s = v3;
            v0 = 1;
        }

        v3_1 = arg9.optInt("allow_push_daemon_monitor", 1);
        if (ToolUtils.isMiui()) {
            v3_1 = 0;
        }

        if (v3_1 != this.g && v3_1 >= 0) {
            this.g = v3_1;
            v0 = 1;
        }

        v3_1 = arg9.optInt("allow_off_alive", 1);
        if (v3_1 != this.h && v3_1 >= 0) {
            this.h = v3_1;
            v0 = 1;
        }

        v3_1 = arg9.optInt("tt_request_push_sender_interval", 10800);
        if (v3_1 != this.h && v3_1 >= 0) {
            this.m = v3_1;
            v0 = 1;
        }

        v3_1 = arg9.optInt("tt_is_not_applog_request_push_sender", 1);
        if (v3_1 != this.l && v3_1 >= 0) {
            this.l = v3_1;
            if (this.l > 0) {
                HashMap v0_1 = new HashMap();
                AppLog.a(((Map) v0_1));
                com.ss.android.newmedia.message.o.a(this.v).a(this.v, ((Map) v0_1), true);
            }

            v0 = 1;
        }

        v3 = arg9.optString("tt_push_wakeup_black_list", "");
        if (!this.j.equals(v3)) {
            this.j = v3;
            v0 = 1;
        }

        com.ss.android.newmedia.i.a v4_1 = com.ss.android.newmedia.i.a.a(this.v);
        v3_1 = arg9.optInt("is_desktop_red_badge_show", 0);
        Logger.d("RedbadgeConfig", "onGetAppData: IS_DESKTOP_RED_BADGE_SHOW = " + v3_1);
        if (v3_1 == v4_1.a || v3_1 < 0) {
            v3_1 = 0;
        } else {
            v4_1.a = v3_1;
            if (v4_1.a == 0) {
                SsRedbadgeManager.inst(v4_1.c).removeRedbageCount();
            }

            v3_1 = 1;
        }

        String v5 = arg9.optString("desktop_red_badge_args", "");
        if (v5 != null && !v5.equals(v4_1.b)) {
            v4_1.b = v5;
            v3_1 = 1;
        }

        v3_1 |= v0;
        OppoPushWindowManager v0_2 = OppoPushWindowManager.a(this.v);
        v4 = arg9.optString("tt_push_pop_window_rule", "");
        if (v4 == null || (v4.equals(v0_2.f))) {
            v0 = 0;
        } else {
            v0_2.f = v4;
            v0 = 1;
        }

        v0 |= v3_1;
        w v3_2 = com.ss.android.newmedia.message.dialog.w.a(this.v);
        v4 = arg9.optString("tt_push_alert_dialog_setting", "");
        if (v4 != null && !v4.equals(v3_2.a)) {
            v3_2.a = v4;
            v2 = 1;
        }

        v3_2.a();
        return (v0 | v2)>0;
    }

    public final void b() {
        new f(this).start();
    }

    static int b(c arg1) {
        return arg1.m;
    }

    static void c(c arg9) {
        long v4;
        String v3;
        String v2;
        Context v1;
        JSONObject v8;
        try {
            v8 = new JSONObject();
            v8.put("from", "onLoadData");
            v1 = arg9.v;
            v2 = "notify_enabled";
            v3 = "value_load";
            if (arg9.t) {
                v4 = 1;
            } else {
                v4 = 0;
            }

            label_11:
            MobClickCombiner.onEvent(v1, v2, v3, v4, -1, v8);
        } catch (Throwable v0) {
            label_17:
            v0.printStackTrace();
        }
    }

    public final void c() {
        this.m();
    }

    public final void d() {
        try {
            if (android.support.design.a.isTextEmpty(AppLog.k())) {
                return;
            }

            if (android.support.design.a.isTextEmpty(AppLog.deviceId())) {
                return;
            }

            RedbadgeSetting.getInstance(this.v).setRedBadgeSessionKey(AppLog.h());
        } catch (Throwable v0) {
            v0.printStackTrace();
        }
    }

    public final boolean e() {
        boolean v0 = this.n > 0 ? true : false;
        return v0;
    }

    public static boolean f() {
        return true;
    }

    public static synchronized boolean g() {
        return PushSetting.getInstance().isAllowSettingsNotifyEnable();
    }

    public final boolean h() {
        boolean v0 = g() ? PushSettingManager.getInstance().isPushNotifyEnable(this.v) : false;
        return v0;
    }

    public static synchronized void i() {
        a = g() ? true : false;
    }

    public final void j() {
        HashMap v0 = new HashMap();
        AppLog.a(((Map) v0));
        if (this.k()) {
            com.ss.android.newmedia.message.o.a(this.v).a(this.v, ((Map) v0), false);
        } else {
            SsPushManager.inst().handleAppLogUpdate(this.v, AppLog.G(), ((Map) v0));
        }

        LocalHttpSetting.getInstance(this.v).setHttpMonitorPort(AppLog.o());
        SsRedbadgeManager.inst(this.v).onLogConfigUpdate();
    }

    public final boolean k() {
        boolean v0 = true;
        if (this.l == -1) {
            BaseAppData.inst();
            this.l = BaseAppData.getAppSettingSp(this.v).getInt("tt_is_not_applog_request_push_sender", 1);
        }

        if (this.l <= 0) {
            v0 = false;
        }

        return v0;
    }

    private static ShutPushType l() {
        ShutPushType v0 = a ? c : ShutPushType.CLOSE_SERVICE;
        return v0;
    }

    private void m() {
        new e(this).start();
    }
}

